<template>
  <div id="container" class="divTdtMap" />
</template>

<script setup lang="ts">
import { onMounted, onUnmounted } from "vue"

let map = null

const initMap = () => {
  // 直接使用全局 AMap 对象
  map = new AMap.Map("container", {
    viewMode: "3D", // 是否为3D地图模式
    zoom: 12, // 初始化地图级别
    center: [103.84998, 30.06991] // 初始化地图中心点位置
  })

  // 路线规划
  initDriving()
}

// 初始化路径规划
const initDriving = () => {
  // 驾车路线规划配置
  const drivingOption = {
    policy: AMap.DrivingPolicy.LEAST_TIME, // 速度优先的策略
    ferry: 1, // 是否可以使用轮渡
    province: "京", // 车牌省份的汉字缩写
    map: map, // 将路线结果显示在地图上
    showTraffic: true // 规划路线显示实时路况
  }

  // 构造驾车路线导航类
  const driving = new AMap.Driving(drivingOption)

  // 起点和终点经纬度
  const startLngLat = [103.85719, 30.02392] // 起点坐标
  const endLngLat = [103.9887, 30.20845] // 终点坐标

  // 根据起终点经纬度规划驾车导航路线
  driving.search(
    new AMap.LngLat(...startLngLat), // 起点经纬度
    new AMap.LngLat(...endLngLat), // 终点经纬度
    function (status, result) {
      if (status === "complete") {
        console.log(status, result, "绘制驾车路线完成")
      } else {
        console.error("获取驾车数据失败：" + result)
      }
    }
  )
}

onMounted(() => {
  window._AMapSecurityConfig = {
    securityJsCode: "fb94b0847c6229dad789b85e2ae26020" // 添加安全配置
  }
  initMap()
})

onUnmounted(() => {
  map?.destroy() // 销毁地图实例
})
</script>

<style scoped lang="scss">
#container {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}
.divTdtMap {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.info {
  width: 150px !important;
  height: 100px !important;
  background-color: #fff;
  text-align: center;
  padding: 10px;
  position: relative;
  border: 1px solid #b9b9b9;
}
.info img {
  width: 30px;
  height: 23px;
  position: absolute;
  left: calc(50% - 15px);
  bottom: -23px;
}
.amap-info-contentContainer {
  width: 150px !important;
  height: 100px !important;
  background-color: #fff;
  text-align: center;
}
.custom-input-card {
  width: 22rem;
}

.custom-input-card .btn:last-child {
  margin-left: 1rem;
}

.content-window-card {
  position: relative;
  width: 23rem;
  padding: 0.75rem 0 0 1.25rem;
  box-shadow: none;
  bottom: 0;
  left: 0;
}

.content-window-card p {
  height: 2rem;
}
::v-deep .title {
  font-family: "AlibabaPuHuiTi20";
  font-weight: 600;
  font-size: 18px;
  color: #000;
}
::v-deep .serial {
  font-family: "AlibabaPuHuiTi20";
  font-weight: 600;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.8);
  display: flex;

  span {
    margin-left: 12px;
    font-size: 16px;
    color: #000000;
  }
}
::v-deep .type {
  margin-top: 12px;
}
::v-deep .mainTitle {
  width: 70px;
}
::v-deep .line {
  width: 374px;
  height: 0px;
  border: 1px solid rgba(0, 0, 0, 0.3);
  margin: 20px 0 15px 0;
}
::v-deep .interior {
  width: 42px;
  height: 22px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 27px 27px 27px 27px;
  text-align: center;
  line-height: 22px;
  color: #000;
  margin: 0 18px 0 10px;
  font-size: 14px;
}
::v-deep .status {
  width: 56px;
  height: 22px;
  background: #059669;
  border-radius: 45px 45px 45px 45px;
  border: 1px solid #059669;
  text-align: center;
  line-height: 22px;
  color: #fff;
  margin: 0 18px 0 10px;
  font-size: 14px;
}
::v-deep .amap-info {
  width: 406px;
  height: 248px;
  background: rgba(255, 0, 0, 0.2);
  box-shadow: inset 0px 0px 36px 0px #ff4a4a;
  border-radius: 8px 8px 8px 8px;
  padding: 15px 20px;
  position: relative;
}
::v-deep .amap-marker-label {
  border: 1px solid #ffffff00;
  background-color: #b9b9b900;
  font-weight: normal;
  font-size: 16px;
  color: #000;
}
::v-deep .input-item {
  width: 232px;
  display: flex;
  justify-content: space-between;
  margin-left: 18%;
}
::v-deep .driving-track {
  width: 92px;
  height: 36px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #61caa9;
  text-align: center;
  line-height: 36px;
  font-family: "AlibabaPuHuiTi20";
  font-weight: normal;
  font-size: 16px;
  color: #059669;
  cursor: pointer;
}
::v-deep .alarm {
  width: 92px;
  height: 36px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #ff9292;
  text-align: center;
  line-height: 36px;
  font-family: "AlibabaPuHuiTi20";
  font-weight: normal;
  font-size: 16px;
  color: #f53f3f;
  cursor: pointer;
}
::v-deep .close-btn {
  width: 24px;
  height: 24px;
  background-image: url("../../../assets/images/icon-close.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: absolute;
  right: 10px;
  top: 0;
  cursor: pointer;
}
::v-deep .title-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}
</style>
